﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using Microsoft.SharePoint;
using System.Web.Caching;
using TNO.Education.Helpers;

namespace TNO.Education.SearchResultBox
{
    public partial class SearchResultBoxUserControl : BaseControl
    {

        #region Properties

        private SearchResultBox _webPart;
        public SearchResultBox WebPart
        {
            get
            {
                if (_webPart == null)
                    _webPart = this.Parent as SearchResultBox;
                return _webPart;
            }
        }
        new const float CacheMinutes = .5f;

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadAllUniversityCombo();

                this.Literal1.Text = (this.Parent as WebPart).Title;
                TxtTraCuu.Attributes.Add("onkeypress", "var kCode = event.keyCode; if(kCode==13) {var butSearch; for(i=0;i<document.getElementsByTagName(\"INPUT\").length;i++){var strvalue = document.getElementsByTagName(\"INPUT\")[i].attributes[\"id\"].value; if(strvalue.indexOf(\"BtnSearch\")!=-1) {butSearch = document.getElementById(strvalue); } } butSearch.click();}");


                pnlTruongCoDiem.Controls.Clear();
                if (WebPart.DisplayUniversity)
                {
                    string key = this.BaseUrl + "_TopNew";
                    var obj = HttpContext.Current.Cache[key];
                    lock (this)
                    {
                        if (obj == null)
                        {
                            obj = DataHelper.GetTopNewUniversity(WebPart.TopNew);
                            HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddSeconds(30), TimeSpan.Zero, CacheItemPriority.Default, null);
                        }
                    }

                    DataTable table = obj as DataTable;
                    if (table.Rows.Count > 0)
                    {
                        using (HtmlHelper htmlHelper = new HtmlHelper())
                        {
                            using (htmlHelper.RenderTag(HtmlTag.Div))
                            {
                                htmlHelper.AddStyle("font-weight: bolder");
                                using (htmlHelper.RenderTag(HtmlTag.Div))
                                {
                                    htmlHelper.WriteLine("Các trường mới có điểm:");
                                    using (htmlHelper.RenderTag(HtmlTag.Span))
                                    {
                                        htmlHelper.AddAttribute("alt", "diem thi moi");
                                        htmlHelper.AddAttribute("src", "/_layouts/images/TNO.Education/New.gif");
                                        htmlHelper.WriteLine(HtmlTag.Img);
                                    }
                                }
                                using (htmlHelper.RenderTag(HtmlTag.Div))
                                {
                                    htmlHelper.AddStyle("display: inline");
                                    using (htmlHelper.RenderTag(HtmlTag.Ul))
                                    {
                                        for (int i = 0; i < table.Rows.Count; i++)
                                        {
                                            using (htmlHelper.RenderTag(HtmlTag.Li))
                                            {
                                                htmlHelper.AddAttribute("href", WebPart.URL + "?u=" + table.Rows[i][Fields.University.Id].ToString());
                                                htmlHelper.WriteLine(HtmlTag.A, table.Rows[i][Fields.University.Title].ToString());
                                            }
                                        }
                                    }
                                }
                            }

                            pnlTruongCoDiem.Controls.Add(new LiteralControl(htmlHelper.ToString()));
                        }
                    }
                }
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (TxtTraCuu.Text.Trim().Length == 0)
                {
                    throw new Exception("Bạn chưa nhập số báo danh");
                }
                else
                {
                    string code = TxtTraCuu.Text.Trim();
                    string university = ddlUniversity.SelectedValue.Trim();
                    Page.Response.Redirect(WebPart.URL + "?SBD=" + code + "&u=" + university);
                }
            }
            catch (Exception Error)
            {
                lbl_error.Text = Error.Message;
            }
        }

        #endregion

        #region Methods

        private void LoadAllUniversityCombo()
        {
            string key = this.BaseUrl + Fields.University.TableName;
            object obj = HttpContext.Current.Cache[key];

            lock (this)
            {
                if (obj == null)
                {
                    obj = DataHelper.GetAllUniversity();
                    HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddSeconds(CacheMinutes), TimeSpan.Zero, CacheItemPriority.Default, null);
                }
            }

            ddlUniversity.DataSource = obj as DataTable;
            ddlUniversity.DataTextField = Fields.University.Title;
            ddlUniversity.DataValueField = Fields.University.Id;
            ddlUniversity.DataBind();
        }

        #endregion

    }
}
